﻿using Ninject;
using System;
using System.Reflection;

namespace Teamwish.Domain.Common
{
    /// <summary>
    /// NInject单例容器工厂
    /// </summary>
    public static class DIFactory
    {
        private static IKernel kernel = null;
        static object obj = new object();
        /// <summary>
        /// 获取di容器的对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T Resolve<T>()
        {
            if (kernel == null)
            {
                CreateKernel();
            }
            return kernel.Get<T>();
        }
        /// <summary>
        /// 获取di容器的对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static object  Resolve(Type type)
        {
            if (kernel == null)
            {
                CreateKernel();
            }
            return kernel.Get(type);
        }

        public static void CreateKernel()
        {
            if (kernel == null)
            {
                lock (obj)
                {
                    if (kernel == null)
                    {
                        kernel = new StandardKernel();
                        kernel.Load(Assembly.Load(new AssemblyName("Teamwish.Domain")));
                    }
                }
            }
        }
    }
}
